import { message} from 'antd'
import router from 'umi/router';
import {createHash} from 'crypto'
export function setRefault(arr) {
    
    if(!arr.code){
        message.error("未知错误");
    }
    if(arr.code < -100){
        message.error(`出现错误：${arr.msg}，请重新登录`);
        localStorage.removeItem('token');
        //去登录
        router.push('/passport/login');
        return [];
    }
    //console.log('arr',arr);
    return arr;
}

export function setParam() {
    
    const timestamp = Number.parseInt(new Date().getTime() / 1000);
    const signStr = `d6aaefd046d48858aa0ef814bcff409c&appid=web_blog&timestamp=${timestamp}&d6aaefd046d48858aa0ef814bcff409c`;
    var param = {
        sign:md5(sha1(signStr)),
        appid:'web_blog',
        timestamp:timestamp
    };
    if(localStorage.getItem('token')){
        param = Object.assign(param,{token:localStorage.getItem('token')});
    }
    return param;
}

/**
 * @param {string} algorithm
 * @param {any} content
 *  @return {string}
 */
export const encrypt = (algorithm, content) => {
    let hash = createHash(algorithm)
    hash.update(content)
    return hash.digest('hex')
}

/**
 * @param {any} content
 *  @return {string}
 */
export const sha1 = (content) => encrypt('sha1', content)

/**
 * @param {any} content
 *  @return {string}
 */
export const md5 = (content) => encrypt('md5', content)